/**
 * Copyright 2011, Under-Ground Studio.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.edu.zju.vlis.aus;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import cn.edu.zju.vlis.aus.db.UsageRecordDAO;
import cn.edu.zju.vlis.aus.util.IconCache;

/**
 * Instance of this class is a splash screen. During the splash, we will:
 * 1.Create and initialize the database if needed. 
 * 2.Load icon of applications and store them in cache.
 * 
 * @author ymsong (songyuming1985@gmail.com) 2011-9-27
 */
public class SplashActivity extends Activity {
	//splash screen time
	private static final int SPLASH_DURATION = 1500;
	
	private View initdbMsgView;
	private View loadIconMsgView;
	private long duration;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash_screen);
		initdbMsgView = findViewById(R.id.splash_msg_initdb);
		initdbMsgView.setVisibility(View.GONE);
		loadIconMsgView = findViewById(R.id.splash_msg_loadicon);
		loadIconMsgView.setVisibility(View.GONE);
	}

	@Override
	protected void onStart() {
		super.onStart();
		duration = System.currentTimeMillis();
		new PreparationTask().execute();
	}

	private class PreparationTask extends AsyncTask<Void, Void, Void> {
		boolean dbCreated;
		UsageRecordDAO dao = null;

		@Override
		protected void onPreExecute() {
			dao = new UsageRecordDAO(SplashActivity.this);
			if (dao.isCreated()) {
				dbCreated = true;
				loadIconMsgView.setVisibility(View.VISIBLE);
			} else {
				dbCreated = false;
				initdbMsgView.setVisibility(View.VISIBLE);
			}
			dao.close();
		}

		@Override
		protected Void doInBackground(Void... params) {
			// create and initialize the data base if needed.
			dao = new UsageRecordDAO(SplashActivity.this);
			if (!dao.isCreated()) {
				dao.createAndInitTable();
			}
			dao.close();
			if (!dbCreated){
				initdbMsgView.post(new Runnable() {
	                public void run() {
	                	initdbMsgView.setVisibility(View.GONE);
	                	loadIconMsgView.setVisibility(View.VISIBLE);
	                }
	            });
			}
			//load all icons
			IconCache.loadDefaultIcon(SplashActivity.this);
			IconCache.loadAllIcons(SplashActivity.this);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			duration = System.currentTimeMillis() - duration;
			if (duration < SPLASH_DURATION) {//start main activity later
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						startMainActivity();
					}
				}, SPLASH_DURATION - duration);
			} else {//start main activity immediately
				startMainActivity();
			}
		}

		private void startMainActivity() {
			// start main activity
			Intent mainIntent = new Intent(SplashActivity.this,
					MainActivity.class);
			SplashActivity.this.startActivity(mainIntent);
			// finish self.
			SplashActivity.this.finish();
//			overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
		}
	}
}
